﻿using SAP.Middleware.Connector;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.Common
{
    public class SAPHelper
    {
        RfcDestination prd = null;
        IDestinationConfiguration ID = null;
        private void PRDInit(string sapName)
        {
            if (ID == null)
            {
                ID = new RFC_Config();
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(ID);
                }
                catch
                {
                }
            }
            if (prd == null)
            {
                //这里控制切换正式/测试环境
                prd = RfcDestinationManager.GetDestination(sapName);
            }
        }
        /// <summary>
        /// 传入表返回表
        /// </summary>
        /// <param name="dt">传入的表-包含数据</param>
        /// <param name="sapReturnTable">sap内表名称</param>
        /// <param name="dtSAPData">返回结果表-需包含空字段</param>
        /// <param name="functionName">SAP调用函数名称</param>
        /// <param name="list">所有需要返回的字段名称</param>
        /// <returns></returns>
        public DataTable GetSAPDataTable(DataTable dt, string sapReturnTable, DataTable dtSAPData, string functionName, List<string> list, string sapName, out string strError)
        {
            string ErrorList = "";
            try
            {
                PRDInit(sapName);
                RfcRepository rfcRep = prd.Repository;
                IRfcFunction func = rfcRep.CreateFunction(functionName);
                IRfcTable tSAP = func.GetTable(sapReturnTable);

                for (int i = 0; i < dt.Rows.Count; i++)

                {
                    IRfcStructure struSAP = tSAP.Metadata.LineType.CreateStructure();
                    foreach (DataColumn col in dt.Columns)
                    {
                        string name = col.ColumnName;
                        string values = dt.Rows[i][name].ToString();
                        struSAP.SetValue(name, values);
                    }
                    tSAP.Append(struSAP);
                }

                func.SetValue(sapReturnTable, tSAP); //table 参数
                //func.SetValue("A", "A");   //单个参数    
                func.Invoke(prd);

                IRfcTable SAPDataTable = func.GetTable(sapReturnTable);

                for (int i = 0; i < SAPDataTable.RowCount; i++)
                {
                    IRfcStructure stru = SAPDataTable[i];
                    DataRow dr = dtSAPData.NewRow();
                    foreach (string col in list)
                    {
                        dr[col] = stru.GetValue(col).ToString();
                    }
                    dtSAPData.Rows.Add(dr);
                }
            }
            catch (Exception ex)
            {
                ErrorList = ex.Message;
            }
            strError = ErrorList;
            return dtSAPData;
        }

        /// <summary>
        /// 回传dataTable-单个参数执行
        /// </summary>
        /// <param name="dt">MES传入表</param>
        /// <param name="functionName">SAP函数名</param>
        /// <param name="list">SAP返回字段</param>
        /// <param name="result_dt">返回结果表</param>
        /// <returns></returns>
        public DataTable GetTable(DataTable dt, string functionName, List<string> list, DataTable result_dt, string sapName)
        {
            try
            {
                PRDInit(sapName);
                RfcRepository rfcRepository = prd.Repository;
                IRfcFunction invoke = rfcRepository.CreateFunction(functionName);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dataRow = result_dt.NewRow();
                    foreach (DataColumn col in dt.Columns)
                    {
                        string name = col.ColumnName;
                        string values = dt.Rows[i][name].ToString();
                        invoke.SetValue(name, values);
                        //插入结果表
                        dataRow[name] = values;
                    }
                    //执行函数
                    invoke.Invoke(prd);

                    //取出SAP回传值插入结果表
                    foreach (string col in list)
                    {
                        dataRow[col] = invoke.GetValue(col).ToString();

                    }
                    result_dt.Rows.Add(dataRow);
                }
            }
            catch (Exception ex)
            {
                string strcatch = ex.Message;
            }
            return result_dt;

        }
    }
}
